//导入request.js请求工具
import request from '@/utils/request.js'
import { defineStore } from "pinia"
import {ref} from 'vue'
//提供调用注册接口的函数
export const userRegisterService =(registerData)=>{
    //借助urlSearchParameters完成传递
    const params=new URLSearchParams()
    for(let key in registerData){
        params.append(key,registerData[key]);
    }
    return request.post('/user/register',params);
}
//提供调用登录接口的函数
 export const userLoginService =(loginData)=>{
    const params=new URLSearchParams()
    for(let key in loginData){
        params.append(key,loginData[key]);
    }
    return request.post('user/login',params)
 }

//获取个人信息
export const userInfoGetService = ()=>{
    return request.get('/user/userInfo');
}
//定义个人中心状态
export const useUserInfoStore = defineStore('userInfo',()=>{
    //1.定义用户信息
    const info = ref({})
    //2.定义修改用户信息的方法
    const setInfo = (newInfo)=>{
        info.value = newInfo
    }
    //3.定义清空用户信息的方法
    const removeInfo = ()=>{
        info.value={}
    }

    return{info,setInfo,removeInfo}
},{
    persist:true
})
//修改头像
export const userAvatarUpdateService=(avatarUrl)=>{
    let params = new URLSearchParams();
    params.append('avatarUrl',avatarUrl)
    return request.patch('/user/updateAvatar',params)
}
//修改个人信息
export const userInfoUpdateService = (userInfo)=>{
    return request.put('/user/update',userInfo)
}
// 更新用户密码
export const updatePassword = (passwordModel) => {
    const tokenStore = useTokenStore()
    return request.patch('/updatePwd', passwordModel, {
      headers: {
        'Authorization': tokenStore.token
      }
    })
  }
